﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace BLLMagiPack.Managers
{
    public class BLLMagiPack
    {
        public string CarpetaFotos 
        {
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory + @"FotosOrdenes\";
            }
        }

        protected void ValidarDependencia(List<String> elementos, string tipoEntidad, string descripcionEntidad, bool generoEntidadEsMasculino)
        {
            string mensaje = "No se puede eliminar. Hay {0} que han sido asignados {3} {1} {2}";

            if (elementos.Count > 0)
            {
                string textoElementos = string.Empty;

                for (int i = 0; i < elementos.Count; i++)
                {
                    textoElementos += elementos[i];

                    if (i == (elementos.Count - 2))
                        textoElementos += " y ";
                    else if (i < (elementos.Count - 2))
                        textoElementos += ", ";
                }

                mensaje = string.Format(mensaje, textoElementos, tipoEntidad, descripcionEntidad, (generoEntidadEsMasculino) ? "al" : "a la");
                throw new BLLMagiPackException(mensaje);
            }
        }

        public static Bitmap ResizeImage(Image image, int width, int height)
        {
            var destRect = new Rectangle(0, 0, width, height);
            var destImage = new Bitmap(width, height);

            destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

            using (var graphics = Graphics.FromImage(destImage))
            {
                graphics.CompositingMode = CompositingMode.SourceCopy;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

                using (var wrapMode = new ImageAttributes())
                {
                    wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                    graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
                }
            }

            return destImage;
        }
    }
}
